/************************************************/
/************************************************/
/*** PART 4.2: DESCRIPTIVE ANALYSIS ***/
/************************************************/
/************************************************/




*** REGRESSION SAMPLE ***

	reg bnode_in_friend biat_ethnic male ageinm braven_sd beyes_sd baseline_tmale tmarried baseline_tchildnum ttenured baseline_tage baseline_texperience bteyes_sd btraven_sd b_gms b_pedagogy b_modern b_extrinsic b_warmth b_competence b_idealism csize refshare astudent urfa i.grade1 i.b_schoolid s_ref_exposure if sample1==1, cluster(b_classid)
	gen reg_sample=e(sample)



*** DESCRIPTIVE STATISTICS ***

	** STUDENT CHARACTERISTICS (HOST AND REFUGEE SEPARATELY)

	eststo clear
	
	global vars male ageinm workingmother workingfather computer internet braven_sd beyes_sd bmath_std bturk_std fethnicbias_sd fphysicalbully_sd fverbalbully_sd ftemper_sd astudent bnode_in_friend bnode_out_friend bnode_in_favorite support_received study_received support_provided study_provided bfriend_total_refugee bsupport_total_refugee bstudy_total_refugee  d_bsbully bsbully

	local count: word count $vars
	mat eb1=J(1,`count',0)
	mat eb2=J(1,`count',0)
	mat eb3=J(1,`count',0)
	mat eb4=J(1,`count',0)
	loc k=1
	foreach i in $vars { 
		summarize `i' if refugee==0 & reg_sample==1
	mat eb1[1,`k']=r(mean)
	mat eb2[1,`k']=r(sd)
		summarize `i' if refugee==1 & reg_sample==1
	mat eb3[1,`k']=r(mean)
	mat eb4[1,`k']=r(sd)
	loc k = `k'+1
	}

	matrix colnames eb1 = $vars
	matrix colnames eb2 = $vars
	matrix colnames eb3 = $vars
	matrix colnames eb4 = $vars
	estpost ttest $vars if reg_sample==1, by(refugee)
	estadd matrix eb1, replace
	estadd matrix eb2, replace
	estadd matrix eb3, replace
	estadd matrix eb4, replace
	estimates store host

	esttab host using "$path2/table1.tex", cells("eb1(fmt(2) label(Mean)) eb2(par fmt(2) label(SD)) eb3(fmt(2) label(Mean)) eb4(par fmt(2) label(SD)) p(fmt(2) label(P-value))") coeflabels( male "\addlinespace[1mm] Male" ageinm "Age in Months" workingmother "\addlinespace[1mm] Working Mother" workingfather "Working Father"  computer "Computer at Home" internet "Internet at Home"  braven_sd "\addlinespace[1mm] Raven Score" beyes_sd "Cognitive Empathy Score" bmath_std "Math Score" bturk_std "Verbal Score" fphysicalbully_sd "Physically Aggressive" fverbalbully_sd "Verbally Disruptive" ftemper_sd "Short-Tempered" astudent "Proportion with learning difficulties" bnode_in_friend "\addlinespace[1mm] Friendship Ties (In-Degree)" bnode_out_friend "Friendship Ties (Out-Degree)" bnode_in_favorite "Nominations for Teacher's Favorite" support_received "Emotional Support Ties (Received)" study_received "Academic Support Ties (Received)" support_provided "Emotional Support Ties (Provided)" study_provided "Academic Support Ties (Provided)" bfriend_total_refugee "Refugee Friendship Nominations" bsupport_total_refugee "Refugee E. Support Nominations" bstudy_total_refugee "Refugee A. Support Nominations" fethnicbias_sd "Ethnic Bias" d_bsbully "\addlinespace[1mm] Proportion Reports Bullying" bsbully "Number of Bullies Reported") refcat(male "\textbf{Student Demographics:}" workingmother "\addlinespace[1.5mm] \textbf{SES Indicators:}" braven_sd "\addlinespace[1.5mm] \textbf{Cognitive \& Behavioral Outcomes:}" bnode_in_friend "\addlinespace[1.5mm] \textbf{Network Variables:}" d_bsbully "\addlinespace[1.5mm] \textbf{Reported Bullying:}", nolabel) noobs replace nonumbers mgroups("Host} & \multicolumn{2}{c}{Refugee", pattern(1 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span) substitute("&\multicolumn{5}{c}{}" "&\multicolumn{1}{c}{}\\\cmidrule(lr){2-3}\cmidrule(lr){4-5} " "&\multicolumn{5}{c}{Host} & \multicolumn{2}{c}{Refugee}          \\" "&\multicolumn{2}{c}{Host} & \multicolumn{2}{c}{Refugee}          " "&\multicolumn{1}{c}{}\\\cmidrule(lr){2-3}\cmidrule(lr){4-5}                                             \\" "&\multicolumn{1}{c}{}\\\cmidrule(lr){2-3}\cmidrule(lr){4-5}                                             ") 
	eststo clear

	** TEACHER AND CLASSROOM CHARACTERISTICS - SUMMARY TABLE
	
	preserve

	keep if sample1==1
	collapse braven baseline_tterm refshare refugee maleshare csize bturk_std bmath_std b_schoolid b_classid urfa biat_ethnic baseline_tmale baseline_tage baseline_texperience tmarried ttenured bteyes bteyes_sd btraven btraven_sd b_pedagogy b_gms b_modern b_extrinsic b_warmth b_idealism b_competence grade1 dist1-dist10 bcoleman_ref1_friend bcoleman_ref1_supportself bcoleman_ref1_studyself baseline_tchildnum bcoleman_ref2_friend bcoleman_ref2_supportself bcoleman_ref2_studyself b_districtid ref_s_exposure bexper btsurvey_16_refprop , by(btname)

	eststo: estpost summarize baseline_tmale baseline_tage tmarried baseline_tchildnum ttenured baseline_texperience baseline_tterm csize refshare bcoleman_ref1_friend bcoleman_ref1_supportself bcoleman_ref1_studyself bcoleman_ref2_friend bcoleman_ref2_supportself bcoleman_ref2_studyself

	esttab using "$path2/table2.tex", cell("mean(fmt(%9.2f) label(Mean)) sd(fmt(%9.2f) label(SD)) min(fmt(%9.2f) label(Min)) max(fmt(%9.2f) label(Max))")  coeflabels( baseline_tmale "\addlinespace[1mm] Male" baseline_tage "Age" tmarried "Married" ttenured "Tenured" baseline_texperience "Years of Experience" baseline_tterm "Number of Semesters in Current Class" baseline_tchildnum "Number of Children"  csize "\addlinespace[1mm] Class Size" refshare "Proportion of Refugees" bcoleman_ref1_friend "Coleman Host Homophily (Friendship)" bcoleman_ref1_supportself "Coleman Host Homophily (Emotional S.)" bcoleman_ref1_studyself "Coleman Host Homophily (Academic S.)" bcoleman_ref2_friend "Coleman Refugee Homophily (Friendship)" bcoleman_ref2_supportself "Coleman Refugee Homophily (Emotional S.)" bcoleman_ref2_studyself "Coleman Refugee Homophily (Academic S.)") refcat(baseline_tmale "\textbf{Teacher Demographics:}" csize "\addlinespace[1.5mm] \textbf{Classroom Variables:}", nolabel)  noobs replace nonumbers
	eststo clear

	

*** PREDICTORS OF TEACHER ETHNIC BIAS ***

	replace refshare=refshare*100
	
	** Refugee Characteristics 
	
	reg biat_ethnic c.refshare csize i.bexper i.urfa i.grade1 i.b_schoolid, cluster(b_classid)
	estimates store spec1a
	scalar i1=e(r2)
	estadd scalar pr2=i1 : spec1a

	** + teacher demographics
	
	reg biat_ethnic c.refshare csize baseline_tmale baseline_tage baseline_texperience tmarried baseline_tchildnum ttenured  btsurvey_16_refprop i.bexper i.urfa i.grade1 i.b_schoolid, cluster(b_classid)
	estimates store spec1b
	scalar i1=e(r2)
	estadd scalar pr2=i1 : spec1b

	** + teacher Cognition
	
	reg biat_ethnic c.refshare csize baseline_tmale baseline_tage baseline_texperience tmarried baseline_tchildnum ttenured  btsurvey_16_refprop btraven_sd bteyes_sd i.bexper i.urfa i.grade1 i.b_schoolid , cluster(b_classid)
	estimates store spec1c
	scalar i1=e(r2)
	estadd scalar pr2=i1 : spec1c

	** + teacher styles

	reg biat_ethnic c.refshare csize baseline_tmale baseline_tage baseline_texperience tmarried baseline_tchildnum ttenured  btsurvey_16_refprop btraven_sd bteyes_sd b_gms b_pedagogy b_modern b_extrinsic b_warmth b_competence b_idealism i.bexper i.urfa i.grade1 i.b_schoolid , cluster(b_classid)
	estimates store spec1d
	scalar i1=e(r2)
	estadd scalar pr2=i1 : spec1d


	#delimit ;
	esttab spec1a spec1b spec1c spec1d  using "$path2/table4.tex",  replace label compress b(%8.2f) se(2) noconstant
	nonumbers keep(refshare  csize baseline_tmale baseline_tage baseline_texperience tmarried baseline_tchildnum ttenured  btsurvey_16_refprop btraven_sd bteyes_sd b_gms b_pedagogy b_modern b_extrinsic b_warmth b_competence b_idealism)
	coeflabels(baseline_tmale "\addlinespace[1mm] Male Teacher" tmarried "Married Teacher" ttenured "Tenured Teacher" baseline_tage "Teacher Age" baseline_texperience "Years of Experience" baseline_tchildnum "Number of Children" bteyes_sd "Teacher Cognitive Empathy Score" btraven_sd "\addlinespace[1mm] Teacher Raven Score" b_gms "\addlinespace[1mm] Growth Mindset" b_pedagogy "Inquiry-based Pedagogy" b_modern "Modern Teaching" b_extrinsic "Extrinsic Motivation" b_warmth "Warmth" b_competence "\addlinespace[1mm] Competence" b_idealism "Professional Satisfaction" csize "Class Size" refshare "Proportion of Refugees" btsurvey_16_refprop "Desired Work Province - \% of refugee")
	star(* 0.10 ** 0.05 *** 0.01) nonotes 
	s(N pr2, fmt(%9.0g %9.3f) labels("N" "R-Squared"))
	nogaps
	mtitles ( "(1)" "(2)" "(3)" "(4)")
	refcat(baseline_tmale "\textbf{Teacher Demographics:}" b_gms "\addlinespace[1.5mm] \textbf{Teaching Styles:}"  btraven_sd "\addlinespace[1.5mm] \textbf{Teacher Cognition:}" b_competence "\textbf{Teacher Motivation:}", nolabel)
	;
	#delimit cr
	eststo clear

	restore
	
*** BALANCE CHECK ***

	reg biat_ethnic braven_sd beyes_sd male ageinm refugee workingmother workingfather computer internet  refshare s_ref_exposure csize i.grade1 i.b_schoolid  if sample1==1, cluster(b_classid)
	estimates store spec5
	scalar i1=e(r2)
	test braven_sd=beyes_sd=male=ageinm=refugee=workingmother=workingfather=computer=internet=refshare=csize=0
	scalar i2=r(p)
	estadd scalar pr1=i2 : spec5
	estadd scalar pr2=i1 : spec5


	forvalues i = 0/1{
	reg biat_ethnic braven_sd beyes_sd male ageinm  workingmother workingfather computer internet  refshare s_ref_exposure csize i.grade1 i.b_schoolid  if refugee==`i' & sample1==1, cluster(b_classid)
	estimates store spec5`i'
	scalar i1=e(r2)
	test braven_sd=beyes_sd=male=ageinm=workingmother=workingfather=computer=internet=refshare=csize=0
	scalar i2=r(p)
	estadd scalar pr1=i2 : spec5`i'
	estadd scalar pr2=i1 : spec5`i'
	}
	
	#delimit ;
	esttab spec5 spec50 spec51  using "$path2/table3.tex",  replace label compress b(%8.2f) se(2) noconstant
	nonumbers keep(braven_sd beyes_sd male ageinm workingmother workingfather computer internet refshare  s_ref_exposure csize)
	coeflabels(braven_sd "Raven Score" beyes_sd "Cognitive Empathy Score" male "Male" ageinm "Student Age in Months" workingmother "Working Mother" workingfather "Working Father" computer "Computer at Home" internet "Internet at Home" refshare "Proportion of Refugees" s_ref_exposure "\# of Semesters exposed to Refugees" csize "Class Size")
	star(* 0.10 ** 0.05 *** 0.01) nonotes 
	s(N pr1 pr2, fmt(%9.0g %9.3f %9.3f) labels("N" "P-Value of Joint Significance" "R-Squared"))
	nogaps
	mtitles ("Full" "Host" "Refugee" )
	refcat(male "\addlinespace[1.5mm] \textbf{Demographics:}" braven_sd "\textbf{Cognition:}"  workingmother "\addlinespace[1.5mm] \textbf{SES Indicators:}" refshare "\addlinespace[1.5mm] \textbf{Classroom Characteristics:}", nolabel)
	;
	#delimit cr	
    eststo clear
